using Cinemachine;
using OneGame.Gameplay.Monster;
using OneGame.Gameplay.Player;
using UnityEngine;
using UnityEngine.InputSystem;

namespace OneGame.Gameplay.Core
{
    public class CombatEntry : MonoBehaviour
    {
        [SerializeField]
        private CinemachineVirtualCamera virtualCamera;

        [SerializeField]
        private Camera mainCamera;

        private void Start()
        {
            PlayerFactory.Create(this, OnPlayerCreated);
        }

        private void OnPlayerCreated(GameObject go)
        {
            var transformComponent = go.transform;
            transformComponent.position = Vector3.zero;
            var playerController = go.GetComponent<PlayerController>();
            playerController.CameraTransform = mainCamera.gameObject.transform;
            virtualCamera.Follow = playerController.cameraFollowTarget;
            MonsterFactory.Create(this, monster => OnMonsterCreated(monster, transformComponent));
        }

        private static void OnMonsterCreated(GameObject go, Transform targetTransform)
        {
            var transformComponent = go.transform;
            transformComponent.position = new Vector3(0, 0, 3);
            transformComponent.rotation = Quaternion.LookRotation(Vector3.back);
            var monsterController = go.GetComponent<MonsterController>();
            monsterController.TargetTransform = targetTransform;
        }

        private void Update()
        {
            if (Keyboard.current.escapeKey.wasPressedThisFrame)
            {
                SceneManager.Instance.LoadScene("LaunchScene");
            }
        }
    }
}